import os
import shutil

def clean_pycache(start_path):
    """
    递归删除所有 __pycache__ 目录
    """
    total_removed = 0
    for root, dirs, files in os.walk(start_path):
        if '__pycache__' in dirs:
            pycache_path = os.path.join(root, '__pycache__')
            print(f'删除目录: {pycache_path}')
            try:
                shutil.rmtree(pycache_path)
                total_removed += 1
            except Exception as e:
                print(f'删除失败: {e}')
    
    if total_removed > 0:
        print(f'\n成功删除 {total_removed} 个 __pycache__ 目录')
    else:
        print('\n未找到 __pycache__ 目录')

if __name__ == '__main__':
    # 获取当前目录
    current_dir = os.path.dirname(os.path.abspath(__file__))
    print('开始清理 __pycache__ 目录...\n')
    clean_pycache(current_dir) 